Introduction to GIS and mapping in R

Cascadia R Conference 2024

Brittany Barker & Roger Andre

2024-06-21

About us: Brittany Barker

Current position
- Asst. Research Professor, OSU

Research interests
- Modeling, ecology, biogeography, conservation

Hobbies
- Running, hiking, biking, moving!
- Gardening, native habitat restoration
- Artistic and creative endeavors

R Community
- Co-organize “Aggregate” meetups
- Co-organize Cascadia R Conference

About us: Roger André

Current position

  • Business Analysis Manager, T-Mobile

Interests

  • Open Source GIS, R Data Science

Hobbies

  • Fly fishing, woodworking

Previous Talks

Outline of workshop

  • ~15 min: introductory lecture
  • Part 1: Introduction to sf package
  • Part 2: Mapping raster and vector data

Source: Zia Ahmed

Elements of Geographic Information System (GIS)

Source: Mierzejowska & Pomyko 2019

Why use R for GIS?

  • Advantages

    • It’s FREE!!!

    • Automate and repeat tasks (faster)

    • Easier to make reproducible workflow

    • Extend functionality

Source: Aleks Buczkowski 2023
  • Disadvantages

    • Steep learning curve

    • Sometimes harder to quickly look at data

Types of spatial features: vector

  • Uses points and/or line segments to identify locations on earth

Different types of vectors (Source: Jakub Nowosad)

R packages for working with vectors

  • sf (simple features)
  • More convenient and flexible than older sp package
  • Integration with the tidyverse
  • Emphasis on the spatial geometry of real world objects

Source: Jakub Nowosad

Types of spatial features: raster

  • Gridded data where each pixel (cell) is associated with a specific geographical location
  • Can be numerical or categorical

Source: Jakub Nowosad

Vector vs. raster

Multi-polygon: provinces of Spain

Source: Paula Moraga

Raster: average annual temperature in Spain

Types of spatial features: raster

  • Multi-layered rasters often called raster stacks
    • Time (day of year, etc.)
    • Different variables

Source: Edzer Pebesama

R packages for working with rasters

  • terra (successor of raster)

    • Create, read/write, manipulate rasters

    • Extensive documentation

  • tidyterra

    • Tidyverse methods for terra objects

    • Provides geoms for plotting in ggplot2

R packages for working with rasters

  • stars(spatiotemporal arrays)

    • Has a few abilities lacking in terra (e.g., non-regular shapes)

    • Many sf functions work with stars objects

    • Not as well documented

Source: Edzer Pebesama

Layering

  • Oftentimes we want to make maps with multiple spatial features

In comes ggplot

  • Uses layered approach to create graphics (The Grammar of Graphics)
  • You provide the data, tell ggplot2 how to map variables to aesthetics, what graphical elements to use, and it takes care of the details

Source: R for the rest of us

In comes ggplot

  • ggplot2 can plot all sorts of data, including rasters and vectors

Source: www.icertglobal.com

Example: a ggplot combining raster and vectors

  • Map of the phenology and potential distribution for invasive emerald ash borer
  • What are the vectors vs. rasters in this map?

Source: Barker et al. (2023) Front. Insect Sci.

Coordinate reference system (CRS)

  • Coordinate system: X, Y grid upon which data is overlaid
  • Units: horizontal & vertical units of grid (e.g., km, miles)
  • Datum: defines origin used to place the coordinate system in space
  • Projection: equation used to flatten objects to 2D surface

Source: Bill Huber, https://www.directionsmag.com

Why is the CRS important?

  • Framework for precisely measuring locations on earth’s surface
  • Errors or mistakes occur when using inconsistent CRS
  • For example, mismatched features and incorrect distance measurements

Source: opennews.org